/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.UserData;
import data.AkademikData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.AkademikDB;

/**
 *
 * @author Muhammad Dony
 */
public class AkademikUserServlet extends HttpServlet {
    UserData listUser;
    AkademikDB akademikdb;
    HttpSession session;
    private String action;
    AkademikData listAkademik;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    
    
    
    protected void halamanAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        UserData user = (UserData) session.getAttribute("userMasuk");
        List listakademik = new AkademikDB().getakademikById(user.getID());
        request.setAttribute("listakademik", listakademik);        
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/view_akademik.jsp");
        rd.forward(request, response);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // login validation start
        session = request.getSession();
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                action = request.getParameter("action");
                if (request.getParameter("action") == null) {
                    try {
                        halamanAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("add")) {
                    halamanTambahAkademik(request, response);
                } else if (action.equals("edit")) {
                    try {
                        halamanEditAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("delete")) {
                    try {
                        deleteAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
                rd.forward(request, response);
            }
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
            rd.forward(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get session
        session = request.getSession();
        // mengambil object sesiUser dari session
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        // mengambil object dengan parameter action dari request
        action = request.getParameter("action");

        if (request.getParameter("action") == null) {
                    try {
                        halamanAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Tambah")) {
                    try {
                        tambahAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Edit")) {
                    try {
                        editAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(AkademikUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                
                }
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public void halamanTambahAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("title", "Tambah Akademik");
        request.setAttribute("buttonName", "Tambah");
        request.setAttribute("listAkademik", new AkademikData());
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/add_akademik.jsp");
        rd.forward(request, response);
    }

    public void halamanEditAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Edit Akademik");
        request.setAttribute("buttonName", "Edit");
        String ID  = request.getParameter("id");
        int no  = Integer.parseInt(request.getParameter("no"));
        List listakademik = new AkademikDB().getakademikByxx(ID, no);
        AkademikData dataAkademik = (AkademikData) listakademik.get(0);
        request.setAttribute("dataAkademik", dataAkademik);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/add_akademik.jsp");
        rd.forward(request, response);
    }

    public void tambahAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listAkademik = new AkademikData();
        // set listUser
        listAkademik.setID(request.getParameter("ID"));
        listAkademik.setSemester(request.getParameter("semester"));
        listAkademik.setIPS(request.getParameter("ips"));
        listAkademik.setIPK(request.getParameter("ipk"));
         listAkademik.setStatus(request.getParameter("status"));
        // memasukkannya dalam database
        akademikdb = new AkademikDB();
        akademikdb.insertAkademik(listAkademik);
        // redirect ke halaman pelaporan
        halamanAkademik(request, response);
    }

    public void editAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listAkademik = new AkademikData();
        // set listUser
        listAkademik.setNomor(Integer.parseInt(request.getParameter("no")));
        listAkademik.setID(request.getParameter("ID"));
        listAkademik.setSemester(request.getParameter("semester"));
        listAkademik.setIPS(request.getParameter("ips"));
        listAkademik.setIPK(request.getParameter("ipk"));
        listAkademik.setStatus(request.getParameter("status"));
        // memasukkannya dalam database
        akademikdb = new AkademikDB();
        akademikdb.updateAkademik(listAkademik);
        // redirect ke halaman pelaporan
        halamanAkademik(request, response);
    }

    
    //ambilnya dari url di bagian view
    public void deleteAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        String ID = request.getParameter("id");
        int akademik  = Integer.parseInt(request.getParameter("akademik"));
        akademikdb = new AkademikDB();
        akademikdb.deleteAkademik(ID, akademik);
        halamanAkademik(request, response);
    }
    
}
